import UIKit
import SystemConfiguration.CaptiveNetwork
import CoreLocation

class WiFiInfoManager: NSObject, CLLocationManagerDelegate {
    private var locationManager: CLLocationManager?

    override init() {
        super.init()
        locationManager = CLLocationManager()
        locationManager?.delegate = self
        locationManager?.requestWhenInUseAuthorization() // 申请在使用时访问位置权限
    }

    func fetchSSIDInfo() -> String? {
        if let interfaces = CNCopySupportedInterfaces() as? [String] {
            for interface in interfaces {
                if let interfaceInfo = CNCopyCurrentNetworkInfo(interface as CFString) as NSDictionary? {
                    return interfaceInfo[kCNNetworkInfoKeySSID as String] as? String
                }
            }
        }
        return nil
    }

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        if status == .authorizedWhenInUse || status == .authorizedAlways {
            if let ssid = fetchSSIDInfo() {
                print("Wi-Fi SSID: \(ssid)")
            } else {
                print("无法获取Wi-Fi名称")
            }
        } else {
            print("未授权访问位置信息")
        }
    }
}


import CoreTelephony

func getCarrierName() -> String? {
    let networkInfo = CTTelephonyNetworkInfo()
    if let carrier = networkInfo.subscriberCellularProvider {
        return carrier.carrierName
    }
    return nil
}


import Foundation
import Network
import CoreTelephony

func getNetworkType() -> String {
    let monitor = NWPathMonitor()
    let semaphore = DispatchSemaphore(value: 0)
    var networkType = "Unknown"
    
    monitor.pathUpdateHandler = { path in
        if path.usesInterfaceType(.wifi) {
            networkType = "Wi-Fi"
        } else if path.usesInterfaceType(.cellular) {
            networkType = "Cellular"
        } else {
            networkType = "Other"
        }
        semaphore.signal()
    }
    
    let queue = DispatchQueue(label: "NetworkMonitor")
    monitor.start(queue: queue)
    
    _ = semaphore.wait(timeout: .now() + 5)  // Wait for a maximum of 5 seconds
    
    return networkType
}
